package proxy;

import java.lang.reflect.Proxy;

/**
 * Description:
 * Designer: jack
 * Date: 2018/3/29
 * Version: 1.0.0
 */
public class Assistant<T> {

    //维护目标对象的Class
    private Class<? extends T> tClass;
    public Assistant(Class<? extends T> tClass) {
        this.tClass=tClass;
    }

    //给目标对象生成代理对象
    @SuppressWarnings("unchecked")
    public T getProxyInstance(){
        return  (T)Proxy.newProxyInstance(
                tClass.getClassLoader(),
                tClass.getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("开始传达指令");
                    //执行目标对象方法
                    return method.invoke(tClass.newInstance(), args);
                }
        );
    }

}
